package ru.lukyanets.transmission.client.ui.controller;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.util.Duration;
import ru.lukyanets.transmission.client.connection.ConnectionState;
import ru.lukyanets.transmission.client.connection.TransmissionService;
import ru.lukyanets.transmission.client.util.LocalizationUtil;
import ru.lukyanets.transmission.protocol.client.exception.TransmissionException;
import ru.lukyanets.transmission.protocol.model.session.Session;
import ru.lukyanets.transmission.protocol.model.session.stats.SessionStats;

import com.google.inject.Inject;
import java.net.URL;
import java.util.ResourceBundle;

import static ru.lukyanets.transmission.client.util.UnitsUtil.getSizeText;
import static ru.lukyanets.transmission.client.util.UnitsUtil.getSpeedText;

/**
 * @author Sergey Lukyanets
 */
public class StatusBarController implements Initializable {
    /** @noinspection unused*/
    @Inject
    private TransmissionService transmissionService;

    @FXML
    private Label connectionState;
    @FXML
    private Label downloadSpeed;
    @FXML
    private Label uploadSpeed;
    @FXML
    private Label freeSize;

    private Timeline timeline;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        timeline = new Timeline(new KeyFrame(Duration.seconds(4), (e) -> refreshStats()));
        timeline.setCycleCount(Timeline.INDEFINITE);

        connectionState.setText(getLocalizedState(transmissionService.getState()));
        transmissionService.addListener((o, oldV, newState) -> Platform.runLater(() -> {
            if (!ConnectionState.CONNECTED.equals(newState)) {
                connectionState.setText(getLocalizedState(newState));
                timeline.stop();
                clearStats();
            } else {
                refreshStats();
                timeline.play();
                try {
                    connectionState.setText(getLocalizedState(transmissionService.getState()) + ": " + transmissionService.getSession().getVersion());
                } catch (TransmissionException ignore) {}
            }
        }));
    }

    private String getLocalizedState(ConnectionState state) {
        return LocalizationUtil.getString("connection.state." + state.name());
    }

    private void refreshStats() {
        try {
            SessionStats stats = transmissionService.getSessionStats();
            Session session = transmissionService.getSession();
            downloadSpeed.setText(LocalizationUtil.getString("session.stats.download.speed") + " " + getSpeedText(stats.getDownloadSpeed()));
            uploadSpeed.setText(LocalizationUtil.getString("session.stats.upload.speed") + " " + getSpeedText(stats.getUploadSpeed()));
            freeSize.setText(LocalizationUtil.getString("session.stats.free.disk") + " " + getSizeText(session.getDownloadDirFreeSpace()));
        } catch (TransmissionException ignore) {
        }
    }

    private void clearStats() {
        downloadSpeed.setText("");
        uploadSpeed.setText("");
        freeSize.setText("");
    }
}
